<template>
  <div>
    <div class="testName">
      <div
        class="backBox"
        @click="backBox"
      >
        <el-icon>
          <ArrowLeft />
        </el-icon>
        <span style="font-size: 16px;color: #363636;">返回</span>
      </div>
      <h1> {{ paperTitle }} </h1>

    </div>
    <div class="testContent">
      <div class="answerSheet">
        <div class="answerSheetTxt">
          <h3>答题卡</h3>
        </div>

        <div class="answerTime">
          <img
            src="@/assets/images/clock.png"
            alt=""
          >
          <p>用时:{{ testTime }}</p>
        </div>

        <div class="answerSituation">
          <p style="text-align: center;padding-right: 25px;">得分：{{ totalScore }}
          </p>

          <!-- 单选题 -->
          <div class="leftSingleChoice">

            <span>单选题</span>
            <span>得分：{{ singleChoiceAssessment }}</span>

            <div class="choiceBox">
              <div
                class="choice"
                v-for="(item,index) in singleChoiceDetail"
                :key="index"
                :class="{
                  rightQuestion:(item.selected == item.rightAnswers) && item.selected !== '',
                  errorQuestion:(item.selected != item.rightAnswers) && item.selected !== '',
                  notDoneQuestion:item.selected == ''
                  }"
              >
                <span>{{ index+1 }}</span>
              </div>
            </div>
          </div>

          <!-- 多选题 -->
          <div class="
                  leftMultipleChoice">
            <span>多选题</span>
            <span>得分：{{ multipleChoiceAssessment }}</span>

            <div class="choiceBox">
              <div
                class="choice"
                v-for="(item,index) in multipleChoiceDetail"
                :key="index"
                :class="{
                  rightQuestion:item.questionStatus === 2,
                  errorQuestion:item.questionStatus === 3,
                  notDoneQuestion:item.questionStatus === 1,
                  elseQuestion:item.questionStatus === 4
                  }"
              >
                <span>{{ index+1 }}</span>
              </div>
            </div>
          </div>

          <!-- 判断题 -->
          <div class="
                leftJudgment">
            <span>判断题</span>
            <span>得分：{{ judgeChoiceAssessment }}</span>

            <div class="choiceBox">
              <div
                class="choice"
                v-for="(item,index) in judgeChoiceDetail"
                :key="index"
                :class="{
                  rightQuestion:item.selected == item.rightAnswers,
                  errorQuestion:(item.selected != item.rightAnswers && item.selected !==''),
                  notDoneQuestion:item.selected == ''
                  }"
              >
                <span>{{ index+1 }}</span>
              </div>
            </div>
          </div>

          <!-- 填空题 -->
          <div class="leftFillInTheBlanks">
            <span>填空题</span>
            <span>得分：{{ fillChoiceAssessment }}</span>

            <div class="choiceBox">
              <div
                class="choice"
                v-for="(item,index) in fillChoiceDetail"
                :key="index"
                :class="{
                  rightQuestion:item.giveMarks == fillChoiceValue,
                  errorQuestion:item.giveMarks == 0,
                  elseQuestion:item.giveMarks < fillChoiceValue,
                  notDoneQuestion:item.inputAnswers = ''
                  }"
              >
                <span>{{ index+1 }}</span>
              </div>
            </div>
          </div>

          <!-- 论述题 -->
          <div class="leftDiscourse">
            <span>论述题</span>
            <span>得分：{{ discussChoiceAssessment }}</span>

            <div class="choiceBox">
              <div
                class="choice"
                v-for="(item,index) in discussChoiceDetail"
                :key="index"
                :class="{
                  rightQuestion:item.giveMarks == fillChoiceValue,
                  errorQuestion:item.giveMarks == 0,
                  elseQuestion:item.giveMarks < fillChoiceValue,
                  notDoneQuestion:item.inputAnswers = ''
                  }"
              >
                {{ index + 1 }}
              </div>
            </div>
          </div>
        </div>
        <div class="answerStatus">
          <div class="rightStatus">
            <div></div><span>正确</span>
          </div>
          <div class="errorStatus">
            <div></div><span>错误</span>
          </div>
          <div class="elseStatus">
            <div></div><span>其他</span>
          </div>
          <div class="notDoneStatus">
            <div></div><span>未做</span>
          </div>
        </div>
      </div>

      <div class="answerDetails">
        <Answerdetails
          :scoreId="scoreId"
          :paperId="paperId"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';

// 引入获取个人成绩详情信息
import { getPersonScoreDetail } from '@/api/assessment/exammanage.js'

// 引入获取试卷详情的接口
import { getTestPaperDetail } from '@/api/assessment/testpapermanage.js'

// 引入考试答题情况的组件
import Answerdetails from './answerDetails.vue'

import moment from 'moment'

const route = useRoute()
const router = useRouter()
// 拿到路由参数中的成绩id
const scoreId = computed(() => route.query.scoreId)

// 拿到路由参数中的试卷id
const paperId = computed(() => route.query.paperId)

function backBox () {
  router.go(-1)
}

const paperTitle = ref('')
const singleChoiceValue = ref(0)
const discussChoiceValue = ref(0)
const multipleChoiceValue = ref(0)
const judgeChoiceValue = ref(0)
const fillChoiceValue = ref(0)
// // 获取当前试卷的详情信息
async function getCurrentPaperDetail () {
  const res = await getTestPaperDetail(paperId.value)
  paperTitle.value = res.data.paperTitle
  singleChoiceValue.value = res.data.singleChoiceValue
  discussChoiceValue.value = res.data.discussChoiceValue
  multipleChoiceValue.value = res.data.multipleChoiceValue
  judgeChoiceValue.value = res.data.judgeChoiceValue
  fillChoiceValue.value = res.data.fillChoiceValue
}
getCurrentPaperDetail()


const singlTestpaperDetail = ref({})
const singleChoiceDetail = ref([])
const singleChoiceAssessment = ref(0)

const multipleChoiceDetail = ref([])
const multipleChoiceAssessment = ref(0)

const judgeChoiceDetail = ref([])
const judgeChoiceAssessment = ref(0)

const fillChoiceDetail = ref([])
const fillChoiceAssessment = ref(0)

const discussChoiceDetail = ref([])
const discussChoiceAssessment = ref(0)

const totalScore = computed(() => {
  return singleChoiceAssessment.value + multipleChoiceAssessment.value + judgeChoiceAssessment.value + fillChoiceAssessment.value + discussChoiceAssessment.value
})

const testStartTime = ref('')
const testEndTime = ref('')

// 获取个人成绩详情
async function getPersonScore () {
  const res = await getPersonScoreDetail(scoreId.value)

  singlTestpaperDetail.value = res.data
  singleChoiceDetail.value = JSON.parse(res.data.singleChoiceDetail)
  multipleChoiceDetail.value = JSON.parse(res.data.multipleChoiceDetail).map(item => {
    let questionStatus = null;

    // 未做
    if (item.selected.length === 0) {
      questionStatus = 1
    }
    // 正确
    else if (item.selected.length === item.rightAnswersMore.length && item.selected.every(answer => item.rightAnswersMore.includes(answer))) {
      questionStatus = 2
    }
    // 错误
    else if (item.selected.some(answer => !item.rightAnswersMore.includes(answer))) {
      questionStatus = 3
    }
    // 不完全正确
    else if (item.selected.length < item.rightAnswersMore.length && item.selected.every(answer => item.rightAnswersMore.includes(answer))) {
      questionStatus = 4
    }
    return {
      ...item,
      rightAnswers: item.rightAnswersMore.split(','),
      questionStatus
    }
  })
  judgeChoiceDetail.value = JSON.parse(res.data.judgeChoiceDetail)
  fillChoiceDetail.value = JSON.parse(res.data.fillChoiceDetail)
  discussChoiceDetail.value = JSON.parse(res.data.discussChoiceDetail)
  singleChoiceAssessment.value = res.data.singleChoiceAssessment
  multipleChoiceAssessment.value = res.data.multipleChoiceAssessment
  judgeChoiceAssessment.value = res.data.judgeChoiceAssessment
  fillChoiceAssessment.value = res.data.fillChoiceAssessment
  discussChoiceAssessment.value = res.data.discussChoiceAssessment

  testStartTime.value = res.data.testStartTime
  testEndTime.value = res.data.testEndTime
}


getPersonScore()

const singleChoiceCount = ref([])

// 计算考试用时
const testTime = computed(() => {
  const startTime = moment(testStartTime.value)
  const endTime = moment(testEndTime.value)
  if (endTime.isBefore(startTime)) {
    return '00:00:00';
  }
  const diffInSeconds = endTime.diff(startTime, 'seconds')
  console.log(diffInSeconds)
  const second = String(diffInSeconds % 60).padStart(2, '0');
  const minute = String(Math.floor(diffInSeconds / 60) % 60).padStart(2, '0');
  const hour = String(Math.floor(diffInSeconds / 3600) % 24).padStart(2, '0');
  return `${hour}:${minute}:${second}`
})

</script>

<style lang='scss' scoped>
.testName {
  display: flex;
  justify-content: center;
  position: relative;
  width: 100%;
  height: 72px;
  background-color: #fff;
  text-align: center;
  align-items: center;
  .backBox {
    display: flex;
    align-items: center;
    position: absolute;
    left: 20px;
    top: 20px;
    cursor: pointer;
    font-size: 16px;
    color: #363636;
  }
}

.testContent {
  position: relative;
  display: flex;
  justify-content: space-between;
  padding: 24px 32px 24px 24px;
  background: #f5f5f5;

  .answerSheet {
    // position: fixed;
    width: 320px;
    min-height: 1000px;
    background: #ffffff;
    border-radius: 6px 6px 6px 6px;
    .answerSheetTxt {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 56px;
      h3 {
        font-weight: bold;
        font-size: 18px;
        color: #363636;
      }
    }

    .answerTime {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 120px;
      border-top: 2px dashed #eeeeee;
      border-bottom: 2px dashed #eeeeee;
      img {
        width: 32px;
        height: 32px;
      }

      p {
        font-size: 16px;
        color: #363636;
      }
    }

    .answerSituation {
      padding: 0 0px 32px 25px;
      display: flex;
      flex-direction: column;
      border-bottom: 2px dashed #d1d1d1;
      p {
        font-weight: bold;
        font-size: 22px;
        color: #f99d38;
      }

      .leftSingleChoice {
        span:nth-child(1) {
          font-size: 14px;
          color: #363636;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #f99d38;
          margin-left: 5px;
        }
      }

      .leftMultipleChoice {
        margin-top: 32px;
        span:nth-child(1) {
          font-size: 14px;
          color: #363636;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #f99d38;
          margin-left: 5px;
        }
      }

      .leftJudgment {
        margin-top: 32px;
        span:nth-child(1) {
          font-size: 14px;
          color: #363636;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #f99d38;
          margin-left: 5px;
        }
      }

      .leftFillInTheBlanks {
        margin-top: 32px;
        span:nth-child(1) {
          font-size: 14px;
          color: #363636;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #f99d38;
          margin-left: 5px;
        }
      }

      .leftDiscourse {
        margin-top: 32px;
        span:nth-child(1) {
          font-size: 14px;
          color: #363636;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #f99d38;
          margin-left: 5px;
        }
      }

      .choiceBox {
        max-width: 220px;
        display: flex;
        flex-wrap: wrap;
        margin-top: 5px;
        .choice {
          width: 32px;
          height: 32px;
          border-radius: 6px;
          margin-top: 10px;
          margin-right: 12px;
          text-align: center;
          line-height: 32px;
          font-size: 14px;
          color: #ffffff;
        }
      }
    }

    .answerStatus {
      display: flex;
      margin-top: 39px;
      padding-left: 25px;
      padding-right: 26px;

      .rightStatus {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: #363636;
        div {
          width: 20px;
          height: 20px;
          margin-right: 5px;
          background-color: #209f85;
          border-radius: 6px;
        }
      }

      .errorStatus {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: #363636;
        margin-left: 10px;
        div {
          width: 20px;
          height: 20px;
          margin-right: 5px;
          background-color: #ea6070;
          border-radius: 6px;
        }
      }

      .elseStatus {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: #363636;
        margin-left: 10px;
        div {
          width: 20px;
          height: 20px;
          margin-right: 5px;
          background-color: #f99d38;
          border-radius: 6px;
        }
      }

      .notDoneStatus {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: #363636;
        margin-left: 10px;
        div {
          width: 20px;
          height: 20px;
          margin-right: 3px;
          // background-color: #ea6070;
          border: 1px solid #209f85;
          border-radius: 6px;
        }
      }
    }
  }

  .answerDetails {
    width: 1512px;
    min-height: 1000px;
    margin-left: 32px;
    background: #ffffff;
    border-radius: 6px 6px 6px 6px;
  }

  .rightQuestion {
    background-color: #209f85;
    span {
      color: #fff !important;
      font-size: 14px;
    }
  }

  .errorQuestion {
    background-color: #ea6070;
    border: none !important;
    span {
      color: #fff !important;
      font-size: 14px;
    }
  }

  .elseQuestion {
    background-color: #f99d38;
    span {
      color: #fff !important;
      font-size: 14px;
    }
  }

  .notDoneQuestion {
    // background-color: transparent;
    border: 1px solid #209f85;
    span {
      color: #363636 !important;
      font-size: 14px;
    }
  }
}
</style>
